<template>
  <div>
    <div>
       <el-form :inline="true" :model="jl" class="user-search">
      <el-form-item label="搜索：">
         <el-input size="small" v-model="jl.code" placeholder="职位种类编码" 
                style="width: 300px"></el-input>
      </el-form-item>
     

      <el-input size="small" v-model="jl.categoryName" placeholder="职位种类名称" 
      style="width: 300px"></el-input>

      <!-- <el-select v-model="jl.categoryName" placeholder="职位级别名称" style="margin-left: 8px" size="small">
        <el-option
            v-for="item in titleLevels"
            :key="item"
            :label="item"
            :value="item">
        </el-option>
      </el-select> -->
    
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-edit" @click="flush">重置</el-button>
       </el-form>
      <el-button size="small" style="margin-left: 8px" icon="el-icon-plus" type="primary" @click="addJobLevel">添加</el-button>
    </div>

    <div style="margin-top: 10px" >
      <el-table
          :data="jls"
          stripe
          border
          size="small"
          style="width: 920px"
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="id"
            label="编号"
            width="120">
        </el-table-column>
        <el-table-column
            prop="code"
            label="种类编码"
            width="150">
        </el-table-column>
        <el-table-column
            prop="categoryName"
            label="种类名称"
            width="150">
        </el-table-column>
        <el-table-column
            prop="enabled"
            label="是否可用"
            width="150">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
            <el-tag type="danger" v-else >未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="remark"
            label="种类描述"
            width="150">
        </el-table-column>

        <el-table-column
            label="操作"
            width="150">
          <template slot-scope="scope">
            <el-button size="small" @click="showEditDialog(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="deleteHandler(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
          <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    </div>
    <el-button type="danger" size="small" style="margin-top: 8px" :disabled="this.multipleSelection.length==0" @click="deleteMany">批量删除</el-button>
    <el-dialog
        title="编辑职称种类"
        :visible.sync="dialogVisible"
        width="30%">
      <table>
        <tr>
          <td>
            <el-tag>种类编码</el-tag>
          </td>
          <td>
            <el-input v-model="updateJl.code" size="small" style="margin-left: 8px"></el-input>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>种类名称</el-tag>
          </td>
          <td>
            <el-input v-model="updateJl.categoryName" size="small" style="margin-left: 8px"></el-input>
            <!-- <el-input v-model="updateJl.categoryName" placeholder="职称等级" style="margin-left: 8px" size="small"></el-input> -->
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>是否启用</el-tag>
          </td>
          <td>
            <el-switch style="margin-left: 8px"
                v-model="updateJl.enabled"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="已启用"
                inactive-text="未启用">
            </el-switch>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>种类描述</el-tag>
          </td>
          <td>
            <el-input v-model="updateJl.remark" size="small" style="margin-left: 8px"></el-input>
          </td>
        </tr>

      </table>
      <span slot="footer" class="dialog-footer">
    <el-button @click="off()">取 消</el-button>
    <el-button type="primary"  @click="doUpdate()">确 定</el-button>
  </span>
    </el-dialog>

  </div>
</template>

<script>
import Pagination from '../../components/Pagination'
export default {
  name: "JobTypeMana" ,
  mounted() {
    this.initJls(this.pageparm);
      },
    // 注册组件
  components: {
    Pagination
  },
   created() {
    // this.getdata(this.formInline)

  },
  data() {
    return { 
      //编辑判断(true)  添加判断(false)
      isCompile: false,
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      },
      updateJl: {
        code:'',
        categoryName: '',
        enabled: false,
        remark: ""
      },
      dialogVisible: false,
      jls: [],
      jl: {
        code: '',
        categoryName: ''
      },
      multipleSelection: []
    }
  },
  methods: {
      // 搜索事件
    search() {
      console.log("4")
      this.callFather(this.pageparm)
    },
    // 重置事件
    flush(){
       this.jl.code='',
       this.jl.categoryName=''
       this.initJls(this.pageparm)
    },
    //分页
    callFather(dada){
     this.initJls(dada)
    },
    deleteMany() {
      this.$confirm('此操作将永久删除[' + this.multipleSelection.length + ']条职称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = '?';
        this.multipleSelection.forEach(item=>{
          ids = item.id 
        this.deleteRequest('/organization/type/'+ids).then(resp => {
        })})
        this.initJls(this.pageparm)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //取消
    off(){
      this.dialogVisible = false;
      this.updateJl.code=''
      this.updateJl.categoryName=''
      this.updateJl.enabled=''
      this.updateJl.remark=''
    },
    //确定
    doUpdate() {
      if(this.isCompile){
      this.putRequest('/organization/type/',this.updateJl).then(resp=>{
          this.initJls();
          this.dialogVisible = false;
          this.updateJl.code=''
          this.updateJl.categoryName=''
          this.updateJl.enabled=false
          this.updateJl.remark=''
      })
      }else{
         if (this.updateJl.code!='' && this.updateJl.categoryName!='') {
           console.log(this.updateJl)
        this.postRequest('/organization/type/',this.updateJl).then(resp=>{
          this.initJls();
          this.dialogVisible = false;
          this.updateJl.code=''
          this.updateJl.categoryName=''
          this.updateJl.enabled=false
          this.updateJl.remark=''
        })
      }else {
        this.$message("字段不能为空！");
        this.dialogVisible = true;
      }   
      }
    },

    //编辑
    showEditDialog(data) {
      this.isCompile=true
      data.createDate = '';
      Object.assign(this.updateJl, data);
      this.dialogVisible = true;
    },

    //删除
    deleteHandler(data) {
      console.log("1",data)
      this.$confirm('此操作将永久删除[' + data.categoryName + ']职称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/organization/type/'+data.id).then(resp => {
          if (resp) {
            this.callFather();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //添加
    addJobLevel() { 
        this.isCompile=false
      this.dialogVisible = true;
    },
    //显示
    initJls(dada) {
      if(dada!=undefined){
            this.pageparm.currentPage=dada.currentPage
            this.pageparm.pageSize=dada.pageSize
      }
      this.getRequest('/organization/type/'+this.pageparm.currentPage+'/'+this.pageparm.pageSize+'?code='+this.jl.code+'&categoryName='+this.jl.categoryName).then(res=>{
              this.jls=res.records
              this.pageparm.currentPage=res.current
              this.pageparm.pageSize=res.size
              this.pageparm.total=res.total
              console.log("3",this.pageparm)
    })
    }
  }
}
</script>

<style scoped>

</style>